﻿////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	SelezTesto
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;

using System.Drawing;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Globalization;

namespace Map_Fvg
{
    
    public partial class SelezTesto : Form
    {
        static public int selezTestoFlag;
        private string font;
        private string colore;
        private string altezza;

        public SelezTesto()
        {
            InitializeComponent();
//            if (selezTestoFlag == 1) this.BackColor = Color.Lavender;
  //          else this.BackColor = Color.LemonChiffon;
        }

        private string[] codici = new string[200];
        private int num_codici;
  
        private void SelezTesto_Load(object sender, EventArgs e)
        {
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            find_codici();
   
            for(int j = 0;j < num_codici;++j) comboBox1.Items.Add(codici[j]);
            string codice = string.Empty;
            int i = ViewQ.i_selected;
            if (i == -1)
            {
                label1.Text = "inserimento testo";
                button1.Visible = false;
                button2.Text = "conferma";
                comboBox1.SelectedIndex = 0;
                codice = (string)comboBox1.SelectedItem;
       //         MessageBox.Show(codice);
                textBox1.Text = find_denominazione(codice);
                textBox2.Text = "";
                textBox3.Text = "0";
                textBox4.Text = ViewQ.HitM.X.ToString();
                textBox5.Text = ViewQ.HitM.Y.ToString();

                string[] words;
                char[] delimiterChars = { ' ', ',' };

                words = colore.Split(delimiterChars);
                byte b1 = byte.Parse(words[0]);
                byte b2 = byte.Parse(words[1]);
                byte b3 = byte.Parse(words[2]);
                byte b4 = byte.Parse(words[3]);

                textBox6.BackColor = Color.FromArgb(b1, b2, b3, b4);


                return;
            }
            int pntSimb = ViewQ.vectShapesT[i].pntSimb;
            codice = ViewQ.vectT[pntSimb].codice;
            int index = comboBox1.FindString(codice);
            comboBox1.SelectedIndex = index;

            textBox1.Text = find_denominazione(codice); 
            textBox2.Text = ViewQ.vectShapesT[i].testo;
            textBox3.Text = ViewQ.vectShapesT[i].GMrotation.ToString(nfi);
            textBox4.Text = ViewQ.vectShapesT[i].XX.X.ToString();
            textBox5.Text = ViewQ.vectShapesT[i].XX.Y.ToString();
            textBox6.BackColor = find_colore();
        }

        private void find_codici()
        {

            using (SQLiteConnection conn = SetConnection())
            {
                int ipnt = 0;
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {

                    string ss = "select codice_testo from testo";

                    cmd.CommandText = ss;

                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            codici[ipnt] = reader.GetString(0).ToString();
                            ++ipnt;
                        }
                    }
                }

                conn.Close();
                num_codici = ipnt;
            }
        }
        
        private string find_denominazione( string codice) 
        {

             using (SQLiteConnection conn = SetConnection())
            {
               string denominazione = string.Empty;
               conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {

                    string ss = "select * from testo WHERE codice_testo = '";
                    ss = string.Concat(ss, codice, "'");

  //                           MessageBox.Show(ss);
                    cmd.CommandText = ss;

                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            denominazione = reader.GetString(1).ToString();
                            font = reader.GetString(2).ToString();
                            colore = reader.GetString(3).ToString();
                            altezza = reader.GetString(4).ToString();

                        }
                    }
                }

                conn.Close();

                return(denominazione);
            }

        }

            private string find_classe(string codice)
            {
                string codex = codice.Substring(0, 1);
                string ss = string.Empty;
                using (SQLiteConnection conn = SetConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand(conn))
                    {
                        string s1 = "select * from classi where codice_classe = '";
                        cmd.CommandText = string.Concat(s1, codex, "'");
                        SQLiteDataReader reader = cmd.ExecuteReader();
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                ss = reader.GetString(1).ToString();
                            }
                        }
                        conn.Close();
                    }
                }
                return(ss);

            }


        private SQLiteConnection SetConnection()
        {
            string DataSource = "Data Source=";
            DataSource = string.Concat(DataSource, FormIniz.cartellaSimb, ";");
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            return (conn);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string codice = codici[comboBox1.SelectedIndex];
            textBox1.Text = find_denominazione(codice);

            textBox6.BackColor = find_colore();

        }

        Color find_colore()
        {
            string[] words;
            char[] delimiterChars = { ' ', ',' };

            words = colore.Split(delimiterChars);
            byte b1 = byte.Parse(words[0]);
            byte b2 = byte.Parse(words[1]);
            byte b3 = byte.Parse(words[2]);
            byte b4 = byte.Parse(words[3]);

            return(Color.FromArgb(b1, b2, b3, b4));

        }

        private void button2_Click(object sender, EventArgs e)
        {
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            int ipnt = 0;
            string codice = codici[comboBox1.SelectedIndex]; 
            int i = ViewQ.i_selected;
            bool bNew = false;
            if (i == -1) bNew = true;

            if (!bNew)
            {
                int pntSimb = ViewQ.vectShapesT[i].pntSimb;
                if (ViewQ.vectT[pntSimb].codice == codice)
                {
                    ipnt = pntSimb;
                    goto L20;
                }
            }

            int j;
            for(j = 0; j < ViewQ.nSimbT; ++j)
            {
                if (ViewQ.vectT[j].codice == codice) break;
            }
            if(j < ViewQ.nSimbT && !bNew) 
            {   
                ViewQ.vectShapesT[i].pntSimb = j;
                ipnt = j;
                goto L20;
            }
            if (ViewQ.nSimbT < ViewQ.max_nSimbT)
            {
                ipnt = ViewQ.nSimbT;
                ++ViewQ.nSimbT;
                ViewQ.vectT[ipnt].codice = codice;
                ViewQ.vectT[ipnt].visib = 1;
                ViewQ.vectT[ipnt].selected = 1;
            }
            else
            {
                MessageBox.Show("procedere al salvataggio");
                return;
            }
            if (i >= 0)
            {
                ViewQ.vectShapesT[i].pntSimb = ipnt;
                goto L20;
            }
//      inserire un nuovo codice i ViewQ.vectT[]
//
            if (ViewQ.nShapesT >= ViewQ.max_nShapesT)
            {
                MessageBox.Show("procedere al salvataggio");
                return;
            }

            i = ViewQ.nShapesT;
            ++ViewQ.nShapesT;
            ViewQ.vectShapesT[i].pntSimb = ipnt;
            ViewQ.vectT[ipnt].classe = find_classe(codice);
            string line = FormIniz.nameElement;
            if(line.Length > 6) line= line = FormIniz.nameElement.Substring(0,6);
            ViewQ.vectT[ipnt].elemento = line;

         L20:         using (SQLiteConnection conn = SetConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand(conn))
                    {
                        cmd.CommandText = "select * from testo where codice_testo = '" + codice + "'";

                        SQLiteDataReader reader = cmd.ExecuteReader();
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                string ss = reader.GetString(2).ToString();
                                
                                ViewQ.vectT[ipnt].iFont = stringToFont(ss);
                                
                                string[] words;
                                char[] delimiterChars = { ' ', ',' };

                                string s1 = reader.GetString(3).ToString();    //  colore
                                words = s1.Split(delimiterChars);
                                byte b1 = byte.Parse(words[0]);
                                byte b2 = byte.Parse(words[1]);
                                byte b3 = byte.Parse(words[2]);
                                byte b4 = byte.Parse(words[3]);
                                ViewQ.vectT[ipnt].icolor = Color.FromArgb(b1, b2, b3, b4);
                                ViewQ.vectT[ipnt].altezza = float.Parse(reader.GetString(4).ToString(),nfi);
                                ViewQ.vectT[ipnt].denom = reader.GetString(1).ToString();
                            }
                        }
                        conn.Close();
                    }
                }

            ViewQ.vectShapesT[i].testo = textBox2.Text;
            ViewQ.vectShapesT[i].GMrotation = float.Parse(textBox3.Text, nfi);  
            ViewQ.vectShapesT[i].XX.X = int.Parse(textBox4.Text); 
            ViewQ.vectShapesT[i].XX.Y = int.Parse(textBox5.Text);
            ViewQ.vectShapesT[i].ncar_testo = textBox2.Text.Length;
            ViewQ.vectShapesT[i].selected = 1;
            if (bNew)
            {
                ViewQ.vectT[ipnt].edizione = 3;
                ViewQ.vectShapesT[i].modifica = 3;
            }
            else
            {
                ViewQ.vectT[ipnt].edizione = 2;
                if(ViewQ.vectShapesT[i].modifica == 0) ViewQ.vectShapesT[i].modifica = 1;
            }
            Save.modificaTesto = 1; 
         
            this.DialogResult = DialogResult.OK;
            this.Close();

        }

        private Font stringToFont(string ss)
        {
            float sizef = 1.0F;
            int ipos = ss.IndexOf("Size=", 0);
            if (ipos > 0)
            {
                int ipos1 = ss.IndexOf(",", ipos);
                int len = ipos1 - ipos - 5;
                string s1 = ss.Substring(ipos + 5, len);
                sizef = float.Parse(s1);
                //              MessageBox.Show(s1 + "  " + sizef.ToString()); 
            }

            Font ifont = new Font(ss, sizef);
            return (ifont);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int i = ViewQ.i_selected;

            string ss = "si vuole cancellare il testo '";
            ss = string.Concat(ss,ViewQ.vectShapesT[i].testo,"' ?");
            string caption = "avviso";

            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult result;

            result = MessageBox.Show(ss, caption, buttons);
            if (result == DialogResult.Yes) 
            {
                ViewQ.vectShapesT[i].modifica = 2;
                Save.modificaTesto = 1; 
                this.DialogResult = DialogResult.OK;
            }
            this.Close();
        }

    }
}
